<?php

namespace App\Modules\Admin\Services\Statistic;

use App\Modules\Admin\Entities\Member\UsersWithApparatuses;
use App\Modules\Admin\Entities\Member\UserWaterIntakeLogModel;
use App\Modules\Admin\Entities\Water\ApparatusElectricityLogModel;
use App\Modules\Admin\Entities\Water\ApparatusPropertyFeeLogModel;
use App\Modules\Admin\Entities\Water\ApparatusPropertyFreeModel;
use App\Modules\Admin\Entities\Water\ApparatusWaterRateLogModel;
use App\Modules\Admin\Entities\Water\WaterDeviceModel;
use App\Modules\Admin\Entities\Water\WaterDeviceMonthModel;
use App\Modules\Admin\Entities\Water\WaterOrderModel;
use App\Modules\Admin\Entities\Water\WaterWithUserModel;
use App\Modules\Admin\Services\BaseService;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use function MongoDB\BSON\toCanonicalExtendedJSON;

class StatisticWaterDeviceService extends BaseService
{
    /**
     * @param $id
     * @param $startTime
     * @param $endTime
     * @param $type
     * @return array
     */
    public function getWaterDeviceList($params){

        $list_device = WaterDeviceModel::getInstance()->get()->toArray();
        $start_date='2025-01-03';
        $get_param = [
            'current'=>1,
            'size'=> 100000000,
            'StartDate'=> '2025-02-28T16:00:00.000Z',
            'EndDate'=> '2025-03-30T16:00:00.000Z',
            'CardNo'=> '',
            'conType'=> '',
        ];

        exit(json_encode($this->getMonth($get_param,$list_device)));
        $uuid = "7e479ba5-ef02-404b-9a34-b1f0012f8c09";
        $days = get_days_range('2025-03-01','2025-03-31');
        foreach ($days as $day){
            $date = \Carbon\Carbon::createFromFormat('Y-m-d', $day);
            $date->subDay(); // 将日期增加一天
            $StartDate = $date->copy()->setTime(16, 0, 0)->format('Y-m-d\TH:i:s.v\Z');
            $get_param['EndDate'] = $StartDate;
            $get_param['StartDate'] = $StartDate;
            foreach ($list_device as $item){
                if ($item['equipment_id']){
                    $get_param['EquipmentName'] = $item['mineral_title'];

                    $result =$this->curl_request_junda($get_param,$uuid);
                    $ConMoney = $result['ConMoney']??0;
                    $rows = $result['rows']??[];


                    WaterDeviceMonthModel::getInstance()->create([
                        'water_id'=>$item['water_id'],
                        'water_title'=>$item['water_title'],
                        'date'=>$day,
                        'day'=>'day',
                        'con_money'=>$ConMoney,
                        'con_type'=>0
                    ]);
//                    exit(json_encode([$day,$ConMoney,$rows]));

                }
            }

        }
        exit(json_encode($days));
        exit(json_encode(get_days_range('2025-03-01','2025-03-31')));




        exit(json_encode($list_device));

    }
    public function getMonth($get_param,$list_device){
        $uuid = "7e479ba5-ef02-404b-9a34-b1f0012f8c09";
        foreach ($list_device as $item){
            if ($item['equipment_id']){
                $get_param['EquipmentName'] = $item['mineral_title'];

                $result =$this->curl_request_junda($get_param,$uuid);
                $ConMoney = $result['ConMoney']??0;
                $rows = $result['rows']??[];


                WaterDeviceMonthModel::getInstance()->create([
                    'water_id'=>$item['water_id'],
                    'water_title'=>$item['water_title'],
                    'date'=>'2025-03-01',
                    'day'=>'month',
                    'con_money'=>$ConMoney,
                    'con_type'=>0
                ]);
//                    exit(json_encode([$day,$ConMoney,$rows]));

            }
        }
        exit(json_encode($get_param));
    }
    public function curl_request_junda($post,$uuid){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://webapi.ljda.net/BusDataCenter/GetConsumeDetailList');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Accept: application/json, text/plain, */*',
            'Accept-Language: zh-CN,zh;q=0.9',
            'Connection: keep-alive',
            'Content-Type: application/json;charset=UTF-8',
            'Origin: http://oa.ljda.net',
            'Referer: http://oa.ljda.net/',
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
            'X-Token: '.$uuid.'',
            'uuid: '.$uuid.'',
        ]);
//        curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"current\":1,\"size\":10,\"EquipmentName\":\"鲍山花园北区小广场\",\"EndDate\":\"2024-12-31T16:00:00.000Z\",\"StartDate\":\"2024-12-31T16:00:00.000Z\",\"CardNo\":\"\",\"conType\":\"\"}");
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($ch);
        curl_close($ch);
        $object = json_decode($response);
        $return = object_to_array($object);

        return $return;
    }
}
